package adfc2.tasks;

import adfc2.actions.TakeAction;
import adfc2.core.Action;
import adfc2.core.Creature;
import adfc2.core.Item;
import adfc2.core.Task;
import adfc2.entities.Workshop;

public class TakeTask extends Task {

    public final Item item;
    private MoveTask moving;

    public TakeTask(Creature actor, Item item) {
        super(actor);
        this.item = item;
    }

    @Override
    public Action nextAction() {
        if (item.owner == actor) {
            completed();
            return null;
        }
        if (item.owner == null || !(item.owner instanceof Workshop)) {//TODO other containers
            failed();
            return null;
        }
        Workshop workshop = (Workshop) item.owner;
        if (actor.getXY() == workshop.getEntranceXY()) {
            return new TakeAction(actor, item);
        } else {
            if (moving == null) {
                moving = new MoveTask(actor, workshop.getEntranceXY(), false);
            }
            return moving.nextAction();
        }
    }

    @Override
    public String shortName() {
        return "take " + item.shortName();
    }
}
